"""
LoginPage页面对象，主要用于实现web系统的登录操作

"""
from base.base_page import BasePage
import selenium.webdriver as webdriver
from selenium.webdriver.common.by import By
import time

# 定义变量
var_user = '18555059369'
var_pwd = '238200'
var_user_xpath = '//*[@id="app"]/div/div[2]/form/div[1]/div/div/input'
var_pwd_xpath = '//*[@id="app"]/div/div[2]/form/div[2]/div/div/input'
var_select_type_xpath = '//*[@id="app"]/div/div[2]/form/div[3]/div/div/div/input'
var_select_target_xpath = '/html/body/div[2]/div[1]/div[1]/ul/li[1]'
var_button_xpath = '//*[@id="app"]/div/div[2]/form/div[4]/div/button'


# 登录页面对象
class LoginPage(BasePage):
    # 页面URL

    url = 'http://hhycs.cargocn.com/#/login'
    print('login:' + url)

    # 页面关键元素
    user = (By.XPATH, var_user_xpath)
    # 18555059369
    pwd = (By.XPATH, var_pwd_xpath)
    # 238200
    select_type = (By.XPATH, var_select_type_xpath)
    select_target = (By.XPATH, var_select_target_xpath)

    button = (By.XPATH, var_button_xpath)

    # 页面的业务流程
    def login(self, u, p):
        self.visit(self.url)
        self.input(self.user, u)
        self.input(self.pwd, p)

        print('运营端!')
        self.select(self.select_type, self.select_target)

        self.click(self.button)


if __name__ == '__main__':
    print('start...')
    print('打开浏览器')
    driver = webdriver.Chrome()
    print('打开登录界面')
    lp = LoginPage(driver)
    print('执行登录')
    lp.login(var_user, var_pwd)
    print('用户' + var_user + '登录成功！')
    print('登录前界面已截图保存！')
    lp.snapshot('login_snapshot.png')
    time.sleep(1)
    lp.snapshot('login_success_snapshot.png')
    print('登录后界面已截图保存！')
    eq = lp.assert_url('http://hhycs.cargocn.com/#/welcome')
    print(eq)
    lp.quit()
